/**
 * 缓冲层: 用于暂存绘图层绘制好的图形
 * @namespace Note.CacheLayer
 * 
 * Note.CacheLayer类继承自抽象接口
 * interface Layer{
 * 		void load(data);
 * 		array save();
 * 		void clear();
 * }
 * 
 * 构造函数
 * 
 * @constructor CacheLayer
 * @author JunEnstein 2017-05-18
 * @param Object canvas canvas元素对象
 */
Note.CacheLayer = function(canvas){
	// canvas元素对象
	this._canvas = canvas;
	// 获取绘图句柄
	this._context = canvas.getContext("2d");
	// 初始化绘图对象
	this._painter = new Note.Painter(this._context, null);
};

/**
 * 载入data数据到对象中
 * @method load
 * @author JunEnstein 2017-05-19
 * @param  Image img 恢复到缓冲层的图片数据
 */
Note.CacheLayer.prototype.load = function(img){
	var self = this;
	img.onload = function(){
		self.clear();
		self._context.drawImage(img, 0, 0, self._canvas.width, self._canvas.height);
	};
};

/**
 * 保存对象数据
 * @method save
 * @author JunEnstein 2017-05-19
 * @return Image 缓冲层的图片数据
 */
Note.CacheLayer.prototype.save = function(){
	return this._canvas.toDataURL("image/png");
};

/**
 * 清空信息
 * @method clear
 * @author JunEnstein 2017-05-19
 */
Note.CacheLayer.prototype.clear = function(){
	this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
};

/**
 * 清空某一块区域
 * @method clearRect
 * @author JunEnstein 2017-05-19
 * @param  Rect{x:int, y:int, width:int, height:int} rect 需要清空的区域
 */
Note.CacheLayer.prototype.clearRect = function(rect){
	this._context.clearRect(rect.x, rect.y, rect.width, rect.height);
};

/**
 * 向缓冲区中追加一个图形
 * @method addShape
 * @author JunEnstein 2017-05-19
 * @param  {paths:[], shape:string} shape 图形数据
 */
Note.CacheLayer.prototype.addShape = function(shape){
	if(shape.shape == "Eraser") return ;
	this._painter['draw'+shape.shape](shape.paths);
}

/**
 * 设置画笔样式
 * @method setPainterStyle
 * @author JunEnstein 2017-05-19
 * @param  {Object} styles 画笔样式
 */
Note.CacheLayer.prototype.setPainterStyles = function(styles){
	this._painter.setStyles(styles);
};